/**
 * Created with JetBrains PhpStorm.
 * User: jiangshuguang
 * Date: 14-9-29
 * Time: 上午11:07
 * To change this template use File | Settings | File Templates.
 */

exports.util = {

    /**
     * 检查电话号码
     * * @param phone
     * @returns {boolean}
     */
     checkPhone:function(phone){
         return /^\d{11}$/.test($.trim(phone));
     },

    /**
     * 检查密码
     * @param password
     * @returns {boolean}
     */
    checkPassword:function(password){
         return !(password.length < 6 || password.length > 16);
    },

    toPercent : function(num , n) {
        if (!num) {
            return 0;
        }
        n = n || 2;
        return ( Math.round( num * Math.pow( 10, n + 2 ) ) / Math.pow( 10, n ) ).toFixed( n );
    },

    formatAmount : function(s, n) {
        if (!s) {
            return 0;
        }
        n = n >= 0 && n <= 20 ? n : 2;
        s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
        var l = s.split(".")[0].split("").reverse(),
            r = s.split(".")[1];
        t = "";
        for(i = 0; i < l.length; i++ ) {
            t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
        }
        if(n > 0) {
            return t.split("").reverse().join("") + "." + r;
        } else {
            return t.split("").reverse().join("");
        }

    },

    /**
     * 返回是整数的债权份额
     * @param  {[type]} num [description]
     * @return {[type]}     [description]
     */
    toCredit:function(num) {
        return this.formatAmount(num,0);
    },

    /**
     * 浮点型保留精度
     * @param src
     * @param pos
     * @returns {number}
     */
    formatFloat : function(src, pos){
        return Math.round(parseFloat(src)*Math.pow(10, pos))/Math.pow(10, pos);
    },

    /**
     * 日期格式时间转化为时间戳
     * @param str
     * @returns {number}
     */
    strToTimeStamp:function(str){
        var date = new Date(str);
        return date.getTime();
    },


    /**
     * 将时间戳转化为时间
     * @param ns 时间戳
     * @param formatA 格式 yyyy-MM-dd hh:mm:ss
     * @returns {string}
     */
    toTime : function(ns,formatA){
        var time,format,o;
        time = new Date(ns*1000);
        format = formatA?formatA:"yyyy-MM-dd";
        o ={
            "M+" : time.getMonth()+1,
            "d+" : time.getDate(),
            "h+" : time.getHours(),
            "m+" : time.getMinutes(),
            "s+" : time.getSeconds(),
            "q+" : Math.floor((time.getMonth()+3)/3),
            "S" : time.getMilliseconds()
        };
        if(/(y+)/.test(format)){
            format = format.replace(RegExp.$1, (time.getFullYear()+"").substr(4 - RegExp.$1.length));
        }
        for(var k in o){
            if(o.hasOwnProperty(k)){
                if(new RegExp("("+ k +")").test(format)){
                    format = format.replace(RegExp.$1, RegExp.$1.length==1 ? o[k] : ("00"+ o[k]).substr((""+ o[k]).length));
                }
            }
        }
        return format;
    },


    /**
     * 判断类型,比typeof instansof靠谱
     * @param source
     * @param type
     * @returns {boolean}
     */
    is:function(source, type){
        return toString.call(source) === '[object ' + type + ']';
    },



    /**
     * 对url进行分析
     */
    getUrlParams:function(){
        var args = {},
            query = location.search.substring(1),
            pairs = query.split("&"),
            href = location.href,
            hashs = href.split("#");
        for(var i = 0; i < pairs.length; i++) {
            var pos = pairs[i].indexOf('=');
            if (pos == -1) continue;
            var argname = pairs[i].substring(0,pos);
            var value = pairs[i].substring(pos+1);
            value = decodeURIComponent(value);
            args[argname] = value;
        }

        if(hashs.length>=2){
            args["_hash"] = hashs[1];
        }else{
            args["_hash"] = "";
        }
        return args;
    }

};